package Chinese.Education.Identify.controller;

import Chinese.Education.Identify.service.VulnerabilityScanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/scan")
public class VulnerabilityScanController {


    @Autowired
    private VulnerabilityScanService vulnerabilityScanService;

    /**
     * 启动扫描任务
     * @param request 扫描请求参数
     * @return 扫描任务信息
     */
    @PostMapping("/start")
    public Map<String, Object> startScan(@RequestBody Map<String, String> request) {
        // 防止空指针异常
        if (request == null) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 400);
            result.put("message", "请求参数不能为空");
            return result;
        }

        String target = request.get("target");
        String scanName = request.get("scanName");

        if (target == null || target.isEmpty()) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 400);
            result.put("message", "扫描目标不能为空");
            return result;
        }

        if (scanName == null || scanName.isEmpty()) {
            scanName = "扫描任务-" + System.currentTimeMillis();
        }

        return vulnerabilityScanService.startRemoteScan(target, scanName);
    }

    /**
     * 获取扫描任务状态
     * @param taskId 任务ID
     * @return 任务状态
     */
    @GetMapping("/status/{taskId}")
    public Map<String, Object> getScanStatus(@PathVariable String taskId) {
        if (taskId == null || taskId.isEmpty()) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 400);
            result.put("message", "任务ID不能为空");
            return result;
        }

        return vulnerabilityScanService.getScanStatus(taskId);
    }

    /**
     * 获取扫描报告
     * @param taskId 任务ID
     * @return 扫描报告
     */
    @GetMapping("/report/{taskId}")
    public Map<String, Object> getScanReport(@PathVariable String taskId) {
        if (taskId == null || taskId.isEmpty()) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 400);
            result.put("message", "任务ID不能为空");
            return result;
        }

        return vulnerabilityScanService.getScanReport(taskId);
    }

    @RequestMapping("/select")
    public Map<String, Object> selectScanResult(@RequestParam(value = "page", defaultValue = "1") int page,
                                                @RequestParam(value = "limit", defaultValue = "10") int limit,
                                                @RequestParam(value = "scanName", required = false) String scanName,
                                                @RequestParam(value = "id", defaultValue = "1") int id) {
        Map<String, Object> map = vulnerabilityScanService.selectScanResult(page, limit, scanName, id);
        return map;
    }

}
